package com.ateam.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ateam.biz.NoticesBiz;
import com.ateam.pojo.Companies;
import com.ateam.pojo.Notices;
import com.ateam.pojo.Users;
import com.ateam.util.DateFactory;

/**
 * @author WuYuan
 * 
 */

@Controller
public class NoticesController {
	@Resource(name = "noticesBiz")
	private NoticesBiz noticesBiz;

	// 载入最新公告
	@ResponseBody
	@RequestMapping("notice.do")
	public Notices getNotice(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Companies currentCompany = (Companies) session.getAttribute("currentCompany");
				return noticesBiz.getNewNotice(currentCompany.getCompanyid());
			} else
				return null;
		} else
			return null;
	}

	//公司创建者发布公告
	@ResponseBody
	@RequestMapping("releasenotice.do")
	public boolean releasenotice(Notices notice, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentCompany") != null) {
			Companies currentCompany = (Companies) session.getAttribute("currentCompany");
			if (session.getAttribute("currentUser") != null) {
				Users user = (Users) session.getAttribute("currentUser");
				notice.setCompanyid(currentCompany.getCompanyid());
				notice.setUserid(user.getUserid());
				notice.setDate(DateFactory.getDateToymd(new Date()));
				if (0 != noticesBiz.addNotice(notice)) {
					return true;
				}
				return false;
			}
			return false;
		}
		return false;
	}
}
